//
//  CNTabBarController.m
//  BeeQuick_19
//
//  Created by 刘家强 on 16/9/22.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNTabBarController.h"
#import "CNNavViewController.h"
#import "CNMineViewController.h"
#import "CNShoppingViewController.h"
#import "CNUserShopCarTools.h"
#import "Test.h"
#import "CNTabBar.h"

@interface CNTabBarController ()

@end

@implementation CNTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加购物车商品改变的通知
    
//    self.tabBar = [CNTabBar sharedTabBar];tabBar
    [self setValue:[CNTabBar sharedTabBar] forKey:@"tabBar"];
    
    
    
    [self addNotification];
    // 添加子空间
    [self addChildViewControllers];
    
    // tabBar的文字显示的颜色
    [self setAppearance];
    

//    [self test];
}


/**
 添加子控制器
 */
- (void)addChildViewControllers {
    
    [self addChildViewControllerWithVCName:@"CNHomeTableViewController" title:@"首页" imgName:@"v2_home"];
    [self addChildViewControllerWithVCName:@"CNMarketViewController" title:@"闪电超市" imgName:@"v2_order"];
    [self addChildViewControllerWithVCName:@"CNShoppingViewController" title:@"购物车" imgName:@"shopCart"];
    [self addChildViewControllerWithVCName:@"CNMineViewController" title:@"我的" imgName:@"v2_my"];
   
   }

- (void)addChildViewControllerWithVCName:(NSString *)VCName title:(NSString *)title imgName:(NSString *)imgName{
    
    // 获取控制器
    UIViewController *vc = [NSClassFromString(VCName) new];
    
    vc.title = title;
    // 设置默认的图标
    vc.tabBarItem.image = [[UIImage imageNamed:imgName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:[imgName stringByAppendingString:@"_r"]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [self addChildViewController:[[CNNavViewController alloc] initWithRootViewController:vc]];
}


/**
 设置tabBar的文字显示的颜色
 */
- (void)setAppearance {
    // 获取tabBar
    UITabBar *tabBar = [UITabBar appearance];
    // 设置文字显示的颜色
    tabBar.tintColor = [UIColor colorWithHex:0x6C6C6C];
    // 设置文字的颜色
    // vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: UIControlState.selected)
}

//添加tabBar
- (UIViewController *)addChildNavControllerWithVCName:(NSString *)VCName title:(NSString *)title imgName:(NSString *)imgName{
    return nil;
}


#pragma mark 添加购物车购买商品数量改变的通知
- (void)addNotification{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shopCarBuyNumberChanged) name:CNShopCarBuyNumberDidChangeNotification object:nil];
}
- (void)shopCarBuyNumberChanged {
    UIViewController *controller = self.childViewControllers[2];
    UITabBarItem *item = controller.tabBarItem;
    NSInteger goodsNumer = [[CNUserShopCarTools sharedInstance] getShopCarGoodsNumber];
    if (goodsNumer == 0) {
        item.badgeValue = nil;
        return;
    }
    item.badgeValue = [NSString stringWithFormat:@"%ld",goodsNumer];
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark  添加测试按键
- (void)test {
    
    dispatch_async(dispatch_get_main_queue(), ^{
        UIButton *but = [UIButton buttonWithType:UIButtonTypeContactAdd];
        [[UIApplication sharedApplication].keyWindow addSubview:but];
        
        but.center = CGPointMake(50, 50);
        
        [but addTarget:self action:@selector(model) forControlEvents:UIControlEventTouchUpInside];
    });
    
}

#pragma mark  测试按键操作
- (void)model {
    [Test test];
}

@end
